package com.me.study.viewmodel

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.GsonUtils
import com.google.gson.reflect.TypeToken
import com.me.study.app.StudyApplication
import com.me.study.app.data.EmojiBean
import com.me.study.app.data.EntranceDataBean
import com.me.study.app.data.EntranceType
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.util.LogUtils
import org.json.JSONObject
import java.io.IOException

/**
 *create author : FZW
 *create date  : 2024/7/4
 */
class SampleViewModel : BaseViewModel() {

    val entranceDataResult = MutableLiveData<ArrayList<EntranceDataBean>>()
    val emojiDataResult = MutableLiveData<ArrayList<EmojiBean>>()

    fun getEntranceData() {
        val list = arrayListOf<EntranceDataBean>()
        list.add(EntranceDataBean("自定义相机", "CameraX", EntranceType.CAMERA_X.VALUE))
        list.add(EntranceDataBean("富文本", "RecyclerView", EntranceType.RvRichEditor.VALUE))
        list.add(EntranceDataBean("动画", "自定义动画效果", EntranceType.BLUETOOTH.VALUE))
        list.add(EntranceDataBean("RecyclerView", "横向滑动更多", EntranceType.RV_HORIZONTAL_MORE.VALUE))
        list.add(EntranceDataBean("表情", "系统emoji表情", EntranceType.EMOJI.VALUE))
        entranceDataResult.postValue(list)
    }

    fun getEmojiData(context: Context) {
        // https://blog.csdn.net/qq_36395052/article/details/117326535
        // https://www.unicode.org/emoji/charts/full-emoji-list.html#1f635_200d_1f4ab  70
        try {
            val inputStream = context.assets.open("emoji.json")
            val fileLength = inputStream.available()
            val buffer = ByteArray(fileLength)
            inputStream.read(buffer)
            inputStream.close()
            val json = String(buffer)
            val jsonObject = JSONObject(json)
            if (jsonObject.has("data")) {
                val jsonData = jsonObject.getString("data")
                val list: ArrayList<EmojiBean> = GsonUtils.fromJson(jsonData.toString(),
                    object : TypeToken<ArrayList<EmojiBean>>() {}.type
                )
                emojiDataResult.postValue(list)
            }
        } catch (e: IOException) {
            e.printStackTrace()
            LogUtils.debugInfo("BaseChat", "catch:${e}")
        }
    }

}
